package diploma.kisel.modelas.domain;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 * Domain class for Application (table - application).
 * 
 * @author Nikolay Kisel
 *
 */
@Entity
@Table(name = "application")
@NamedQueries({
	@NamedQuery(name = "Application.findAll", query = "select a from Application a"),
	@NamedQuery(name = "Application.findById", query = "select a from Application a where a.id = :id")})
public class Application {

	private Integer id;
	private String number;
	private String name;
	private String shortName;
	private Integer cost;

	// for many to many mapping
	private Set<Workplace> workplaces = new HashSet<Workplace>();
	
	public Application() {
		
	}
	
	public Application(String number, String name, String shortName,
			Integer cost) {
		this.number = number;
		this.name = name;
		this.shortName = shortName;
		this.cost = cost;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id_application")
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "number")
	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	@Column(name = "name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "short_name")
	public String getShortName() {
		return shortName;
	}

	public void setShortName(String shortName) {
		this.shortName = shortName;
	}

	@Column(name = "cost")
	public Integer getCost() {
		return cost;
	}

	public void setCost(Integer cost) {
		this.cost = cost;
	}

	@ManyToMany
	@JoinTable(name = "list_application", joinColumns = @JoinColumn(name = "id_application"), inverseJoinColumns = @JoinColumn(name = "id_workplace"))
	public Set<Workplace> getWorkplaces() {
		return workplaces;
	}

	public void setWorkplaces(Set<Workplace> workplaces) {
		this.workplaces = workplaces;
	}	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((number == null) ? 0 : number.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Application other = (Application) obj;
		if (number == null) {
			if (other.number != null)
				return false;
		} else if (!number.equals(other.number))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Application [id=" + id + ", number=" + number + ", name="
				+ name + ", shortName=" + shortName + ", cost=" + cost + "]";
	}	

}
